<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var waysToMakeFair = function (nums) {
      const len = nums.length
      let n = 0
      let odd = 0 // 总奇数和
      let even = 0 // 总偶数和
      let leftOdd = 0 // 左边奇数和
      let leftEven = 0 // 左边偶数和
      // 求得未删除前的奇偶数和
      for (let i = 0; i < len; i++) {
        if (i % 2 === 0) {
          even += nums[i]
        } else {
          odd += nums[i]
        }
      }
      for (let i = 0; i < len; i++) {
        // 如果是偶数
        if (i % 2 === 0) {
          // 左偶数和 + 总奇数和 - 左奇数和 === 左奇数和 + 总偶数和 - 左偶数和 - 当前数
          if (leftEven + odd - leftOdd === leftOdd + even - leftEven - nums[i]) {
            n += 1
          }
          leftEven += nums[i]
        }
        // 如果是奇数
        else {
          // 左偶数和 + 总奇数和 - 左奇数和 - 当前数 === 左奇数和 + 总偶数和 - 左偶数和
          if (leftEven + odd - leftOdd - nums[i] === leftOdd + even - leftEven) {
            n += 1
          }
          leftOdd += nums[i]
        }
      }
      return n
    };
    console.log(waysToMakeFair(nums = [2, 1, 6, 4]))
  </script>
</body>

</html>